//------------------------------------------------------------------------------ 
// <copyright file="JTab.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Single Tab
    /// </summary>
    [ToolboxData("<{0}:JTab runat=server></{0}:JTab>")]
    [DefaultProperty("HeaderText")]
    [NonVisualControl]
    public class JTab : JQueryBaseControl, IBaseContainer, INamingContainer
    {
        #region Fields
        /// <summary>
        /// Indicates whether execute data bind
        /// </summary>
        private bool trequiredBind = true;
        #endregion

        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JTab class.
        /// </summary>
        public JTab()
        {
            this.IsRenderControl = true;
        }
        #endregion

        #region Property

        /// <summary>
        /// Gets or sets Header Text
        /// </summary>
        public string HeaderText 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets Content Template Control
        /// </summary>
        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(JTab))]
        public virtual ITemplate ContentTemplate
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets header container
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Control HeaderContainer
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets content container
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Control ContentContainer
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets address of web page
        /// </summary>
        public string Url 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets a value indicating whether tab contains web page
        /// </summary>
        internal bool IsWebPage
        {
            get
            {
                return !string.IsNullOrEmpty(this.Url);
            }
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
        }
        #endregion

        #region Override
        /// <summary>
        /// Binds a data source to the invoked server control and all its child controls.
        /// </summary>
        public override void DataBind()
        {
            this.CreateChildControls();
            this.ChildControlsCreated = true;
            this.trequiredBind = false;

            base.DataBind();
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            this.HeaderContainer = new PlaceHolder();

            ITemplate htemplate = new HeaderTabTemplate();

            htemplate.InstantiateIn(this.HeaderContainer);

            this.Controls.Add(this.HeaderContainer);

            if (!this.IsWebPage)
            {
                this.ContentContainer = new PlaceHolder();
                ITemplate ctemplate = this.ContentTemplate ?? new ContentTabTemplate();

                ctemplate.InstantiateIn(this.ContentContainer);

                this.Controls.Add(this.ContentContainer);
            }
        }

        /// <summary>
        /// Raises the System.Web.UI.Control.PreRender event.
        /// </summary>
        /// <param name="e">An System.EventArgs object that contains the event data</param>
        protected override void OnPreRender(System.EventArgs e)
        {
            if (this.trequiredBind && !this.Page.IsPostBack)
            {
                this.DataBind();
                this.trequiredBind = false;
            }

            base.OnPreRender(e);
        }
        #endregion
    }
}